<?php

/**
 * commentaire actions.
 *
 * @package    explications_informatiques
 * @subpackage gifts
 * @author     Guimezanes Cyrille
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class giftsActions extends sfActions
{

  public function executeIpn(sfWebRequest $request)
  {
		// Read the post from PayPal and add 'cmd' 
		$req = 'cmd=_notify-validate'; 
		if(function_exists('get_magic_quotes_gpc')) 
		  {
			$get_magic_quotes_exits = true; 
		  }
				foreach ($_POST as $key => $value) 
				// Handle escape characters, which depends on setting of magic quotes 
				  {
						if($get_magic_quotes_exists == true && get_magic_quotes_gpc() == 1){  
						$value = urlencode(stripslashes($value)); 
					} else { 
					$value = urlencode($value); 
					}
					$req .= "&$key=$value"; 
				}
		// post back to PayPal system to validate
		$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
		$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
		$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
		//$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
		$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
		if (!$fp) {
			 $gift = new Gifts();
			 $gift->setResponse('HTTP_ERROR');
			 $gift->setDate(date("Ymdhis"));
			 $gift->save();
			 
		} else { 
		// NO HTTP ERROR 
		fputs ($fp, $header . $req); 
			while (!feof($fp)) { 
				$res = fgets ($fp, 1024); 
				if (strcmp ($res, "VERIFIED") == 0) { 
					 $gift = new Gifts();
					 if($_POST["custom"] != "-1")
					 	$gift->setIdClientasso($_POST["custom"]);
					 $gift->setValGift($_POST['mc_gross'].$_POST['payment_gross']);
					 $gift->setResponse($_POST['payment_status']);
					 $gift->setPayeurEmail($_POST['payer_email']);
					 $gift->setPayeurId($_POST['payer_id']);
					 $gift->setCurrency($_POST['mc_currency']);
					 $gift->setResent($_POST['resend']);
					 $gift->setDate(date("Ymdhis"));
					 $gift->save();
					
				}
				else if (strcmp ($res, "INVALID") == 0) {
					$gift = new Gifts();
					$gift->setResponse('INVALID');
					$gift->setDate(date("Ymdhis"));
					$gift->save();
				}
			
			}
		}
		fclose ($fp); 
	  die("Fin!");
  }
  public function executeMerci(sfWebRequest $request)
  {
	
  }
  public function executeAnnulation(sfWebRequest $request)
  {
	
  }

}
